package cn.edu.qzuie.artist.module.td.service.worksimage;

import cn.edu.qzuie.artist.module.td.dal.dataobject.work.WorkDO;
import cn.edu.qzuie.artist.module.td.dal.mysql.work.WorkMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.edu.qzuie.artist.module.td.controller.admin.worksimage.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.worksimage.WorksImageDO;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;

import cn.edu.qzuie.artist.module.td.dal.mysql.worksimage.WorksImageMapper;

import static cn.edu.qzuie.artist.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.edu.qzuie.artist.module.td.enums.ErrorCodeConstants.*;

/**
 * 作品集 Service 实现类
 *
 * @author 泉信管理员
 */
@Service
@Validated
public class WorksImageServiceImpl extends ServiceImpl<WorksImageMapper, WorksImageDO> implements WorksImageService {

    @Resource
    private WorksImageMapper worksImageMapper;
    @Resource
    private WorkMapper workMapper;
    @Override
    @Transactional(rollbackFor = Exception.class) // 添加事务
    public Long createWorksImage(WorksImageSaveReqVO createReqVO) {

        // 判断一下如果传过来的图片是空的,将work_id置空(就可以删除作品了)
        if (createReqVO.getImgSrc() == null || createReqVO.getImgSrc().isEmpty()) {
            worksImageMapper.updateByWorkId(createReqVO.getWorkId());
            return createReqVO.getId();
        }
        // 先批量删除
        worksImageMapper.deleteByWorkId(createReqVO.getWorkId());
        // 批量插入图片
//        for (String imgSrc : createReqVO.getImgSrcs()) {
//            WorksImageDO worksImage = new WorksImageDO();
//            worksImage.setWorkId(createReqVO.getWorkId()); // 插入id
//            worksImage.setImgSrc(imgSrc); // 插入图片
//            worksImage.setDescribes(createReqVO.getDescribes());
//            worksImageMapper.insert(worksImage);
//        }

//        WorksImageDO worksImage = BeanUtils.toBean(createReqVO, WorksImageDO.class);
//        worksImageMapper.insert(worksImage);
        // 返回
        return createReqVO.getId();
    }

    @Override
    public void updateWorksImage(WorksImageSaveReqVO updateReqVO) {
        // 校验存在
        validateWorksImageExists(updateReqVO.getId());
        // 更新
        WorksImageDO updateObj = BeanUtils.toBean(updateReqVO, WorksImageDO.class);
        worksImageMapper.updateById(updateObj);
    }

    @Override
    public void deleteWorksImage(Long id) {
        // 校验存在
        validateWorksImageExists(id);
        // 删除
        worksImageMapper.deleteById(id);
    }

    private void validateWorksImageExists(Long id) {
        if (worksImageMapper.selectById(id) == null) {
            throw exception(WORKS_IMAGE_NOT_EXISTS);
        }
    }

//    @Override
//    public WorksImageRespVO getWorksImage(Long workId) {
//
////        // 1.先根据workId查询td_works_image表中的除了img_src的所有数据
////        List<WorksImageDO> worksImageList = worksImageMapper.getByWorkId(workId);
////        // 2.取列表中第一个数据
////        WorksImageDO worksImage = worksImageList.get(0);
////        // 3.如果列表中的describes为空，则将describes赋值为无
////        if (worksImage.getDescribes() == null) {
////            worksImage.setDescribes("无");
////        }
////        // 4.查询 td_work_image 表中的所有相关图片数据
////        List<WorksImageDO> ImageList = getByWorkId(workId);
////        // 5.提取所有图片路径
////        List<String> imageUrls = ImageList.stream()
////                .map(WorksImageDO::getImgSrc)
////                .toList();
//        // 6.实例化WorksImageRespVO对象
//        WorksImageRespVO worksImageRespVO = new WorksImageRespVO();
//        // 7.查询td_work表,获取作品名称
//        WorkDO workDO = workMapper.selectById(workId);
//        worksImageRespVO.setWorkName(workDO.getWorkName()); // 设置作品名称
////        worksImageRespVO.setImgSrcs(imageUrls); // 设置图片列表
//        BeanUtils.copyProperties(workDO, worksImageRespVO); //对象属性拷贝
//
//        return worksImageRespVO;
//    }
    @Override
    public WorksImageDO getWorksImage(Long id) {
    return worksImageMapper.selectById(id);
}


    @Override
    public PageResult<WorksImageDO> getWorksImagePage(WorksImagePageReqVO pageReqVO) {
        return worksImageMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WorksImageDO> getByWorkId(Long workId) {
        QueryWrapper<WorksImageDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_id", workId); // 条件：work_id 等于指定值
        return worksImageMapper.selectList(queryWrapper);

    }

    /**
     * 添加子作品
     * @param createReqVO
     * @param workId
     * @return
     */
    @Override
    public Long createWorksImageWithWorkId(WorksImageSaveReqVO createReqVO, Long workId) {
        // 限制一下顺序
        // 1.先查询作品集的数量
        List<WorksImageDO> worksImageList = worksImageMapper.getByWorkId(workId);
        // 2.如果作品集的数量大于0，则将排序顺序设置为最后一个作品集的排序顺序加1
        if (worksImageList.size() > 0){
            createReqVO.setSortOrder(worksImageList.get(worksImageList.size() - 1).getSortOrder() + 1);
        }
        // 获取作品名称
        String workName = worksImageList.get(0).getWorkName();
        // 设置作品名称
        createReqVO.setWorkName(workName);
        // 设置作品id
        createReqVO.setWorkId(workId);
        // 将createReqVO转换为WorksImageDO对象
        WorksImageDO worksImage = BeanUtils.toBean(createReqVO, WorksImageDO.class);
        // 插入数据
        worksImageMapper.insert(worksImage);
        // 返回作品id
        return createReqVO.getId();
    }

}